package com.ischoolbar.programmer.servlet;

/**
 * @Author lin
 **/

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.ischoolbar.programmer.dao.TeacherDao;
import com.ischoolbar.programmer.model.Page;
import com.ischoolbar.programmer.model.Teacher;
import com.ischoolbar.programmer.util.SnGenerateUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeacherServlet extends HttpServlet {
    private static final long serialVersionUID = -8421947373714720118L;

    public TeacherServlet() {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");
        if ("toTeacherListView".equals(method)) {
            this.teacherList(request, response);
        } else if ("AddTeacher".equals(method)) {
            this.addTeacher(request, response);
        } else if ("TeacherList".equals(method)) {
            this.getTeacherList(request, response);
        } else if ("EditTeacher".equals(method)) {
            this.editTeacher(request, response);
        } else if ("DeleteTeacher".equals(method)) {
            this.deleteTeacher(request, response);
        }

    }

    private void deleteTeacher(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids[]");
        String idStr = "";
        String[] var5 = ids;
        int var6 = ids.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String id = var5[var7];
            idStr = idStr + id + ",";
        }

        idStr = idStr.substring(0, idStr.length() - 1);
        TeacherDao teacherDao = new TeacherDao();
        if (teacherDao.deleteTeacher(idStr)) {
            try {
                response.getWriter().write("success");
            } catch (IOException var12) {
                var12.printStackTrace();
            } finally {
                teacherDao.closeCon();
            }
        }

    }

    private void editTeacher(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        int id = Integer.parseInt(request.getParameter("id"));
        String sex = request.getParameter("sex");
        String mobile = request.getParameter("mobile");
        String qq = request.getParameter("qq");
        int clazzId = Integer.parseInt(request.getParameter("clazzid"));
        Teacher teacher = new Teacher();
        teacher.setClazzId(clazzId);
        teacher.setMobile(mobile);
        teacher.setName(name);
        teacher.setId(id);
        teacher.setQq(qq);
        teacher.setSex(sex);
        TeacherDao teacherDao = new TeacherDao();
        if (teacherDao.editTeacher(teacher)) {
            try {
                response.getWriter().write("success");
            } catch (IOException var15) {
                var15.printStackTrace();
            } finally {
                teacherDao.closeCon();
            }
        }

    }

    private void getTeacherList(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("teacherName");
        Integer currentPage = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("rows") == null ? 999 : Integer.parseInt(request.getParameter("rows"));
        Integer clazz = request.getParameter("clazzid") == null ? 0 : Integer.parseInt(request.getParameter("clazzid"));
        int userType = Integer.parseInt(request.getSession().getAttribute("userType").toString());
        Teacher teacher = new Teacher();
        teacher.setName(name);
        teacher.setClazzId(clazz);
        if (userType == 3) {
            Teacher currentUser = (Teacher)request.getSession().getAttribute("user");
            teacher.setId(currentUser.getId());
        }

        TeacherDao teacherDao = new TeacherDao();
        List<Teacher> teacherList = teacherDao.getTeacherList(teacher, new Page(currentPage, pageSize));
        int total = teacherDao.getTeacherListTotal(teacher);
        teacherDao.closeCon();
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> ret = new HashMap();
        ret.put("total", total);
        ret.put("rows", teacherList);

        try {
            String from = request.getParameter("from");
            if ("combox".equals(from)) {
                response.getWriter().write(JSONArray.fromObject(teacherList).toString());
            } else {
                response.getWriter().write(JSONObject.fromObject(ret).toString());
            }
        } catch (IOException var14) {
            var14.printStackTrace();
        }

    }

    private void addTeacher(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String mobile = request.getParameter("mobile");
        String qq = request.getParameter("qq");
        int clazzId = Integer.parseInt(request.getParameter("clazzid"));
        Teacher teacher = new Teacher();
        teacher.setClazzId(clazzId);
        teacher.setMobile(mobile);
        teacher.setName(name);
        teacher.setPassword(password);
        teacher.setQq(qq);
        teacher.setSex(sex);
        teacher.setSn(SnGenerateUtil.generateTeacherSn(clazzId));
        TeacherDao teacherDao = new TeacherDao();
        if (teacherDao.addTeacher(teacher)) {
            try {
                response.getWriter().write("success");
            } catch (IOException var15) {
                var15.printStackTrace();
            } finally {
                teacherDao.closeCon();
            }
        }

    }

    private void teacherList(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("view/teacherList.jsp").forward(request, response);
        } catch (ServletException var4) {
            var4.printStackTrace();
        } catch (IOException var5) {
            var5.printStackTrace();
        }

    }
}

